home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / demos / earth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.7 KB  |  255 lines

  1. /* Copyright (c) Silicon Graphics, Inc. 1996 */
  2.  
  3. /* earth.c 
  4.  * This program maps a texture of the earth onto a sphere.
  5.  * It uses a quadric object to create the sphere and must 
  6.  * tell the quadric to generate texture coordinates.
  7.  * 
  8.  * The texture environment mode can be changed interactively.
  9.  *
  10.  *    <e> Key        - cycle through environment modes
  11.  *    <s> Key        - toggle earth rotation on/off
  12.  *    <t> Key        - toggle texturing on/off
  13.  *    Escape Key    - exit program
  14.  */
  15. #include <GL/gl.h>
  16. #include <GL/glu.h>
  17. #include <GL/glut.h>
  18.  
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. #include "rgbImageFile.h"    /* should be in ../../include */
  24.  
  25. /*  Function Prototypes  */
  26.  
  27. GLvoid  initgfx( GLvoid );
  28. GLvoid  animate( GLvoid );
  29. GLvoid  visibility( GLint );
  30. GLvoid  drawScene( GLvoid );
  31. GLvoid  reshape( GLsizei, GLsizei );
  32. GLvoid  keyboard( GLubyte, GLint, GLint );
  33.  
  34. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  35.  
  36. void resetView( GLvoid );
  37. void printHelp( char * );
  38.  
  39. /* Global Definitions */
  40.  
  41. #define KEY_ESC    27    /* ascii value for the escape key */
  42.  
  43. /* Global Variables */
  44.  
  45. static GLfloat spin = 0.0;
  46. static GLuint envmode = 0;
  47.  
  48. static GLUquadricObj *quadObj;
  49.  
  50. typedef struct {
  51.     GLint    type;
  52.     char    *name;
  53. } EnvModeInfo;
  54.  
  55. static EnvModeInfo modes[4] = {
  56.     { GL_DECAL, "GL_DECAL" },
  57.     { GL_MODULATE, "GL_MODULATE" },
  58.     { GL_BLEND, "GL_BLEND" },
  59.     { GL_REPLACE_EXT, "GL_REPLACE_EXT" },
  60. };
  61.  
  62. static GLboolean spinFlag = GL_TRUE;
  63. static GLboolean textureFlag = GL_TRUE;
  64.  
  65. void
  66. main ( int argc, char *argv[])
  67. {
  68.     GLsizei     width, height;
  69.     char        *imageFileName = "earth.rgb";
  70.     unsigned int     *image;
  71.     GLsizei        imageWidth, imageHeight;
  72.     
  73.     glutInit( &argc, argv );
  74.  
  75.     image = rgbReadImageFile(imageFileName, &imageWidth,
  76.              &imageHeight);
  77.  
  78.     if ( image == NULL ) {
  79.         fprintf( stderr, "%s: Error reading %s\n", argv[0], imageFileName );
  80.         exit(-1);
  81.     }
  82.  
  83.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  84.     height = glutGet( GLUT_SCREEN_HEIGHT );
  85.     glutInitWindowPosition( width / 4, height / 4 );
  86.     glutInitWindowSize( width / 2, height / 2 );
  87.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
  88.     glutCreateWindow( argv[0] );
  89.  
  90.     initTexture( image, imageWidth, imageHeight );
  91.     initgfx();
  92.  
  93.     glutKeyboardFunc( keyboard );
  94.     glutIdleFunc( animate );
  95.     glutVisibilityFunc( visibility );
  96.     glutReshapeFunc( reshape );
  97.     glutDisplayFunc( drawScene ); 
  98.  
  99.     printHelp( argv[0] );
  100.  
  101.     glutMainLoop();
  102. }
  103.  
  104. void
  105. printHelp( char *progname )
  106. {
  107.     fprintf(stdout, "\n%s - demonstrates texture environment modes\n\n" 
  108.         "<e> Key    - cycle through texture environment modes\n"
  109.         "<s> Key    - toggle earth rotation on/off\n"
  110.         "<t> Key    - toggle texturing on/off\n"
  111.         "Escape Key    - exit the program\n\n",
  112.         progname);
  113.     printf("\nTexture Environment Mode is %s\n", modes[envmode].name);
  114. }
  115.  
  116. GLvoid 
  117. initgfx()
  118. {
  119.     /* infinite light shining from +x to origin */
  120.     GLfloat light_pos[] = {  1.0, 0.0, 0.0, 0.0 };
  121.     GLfloat mat[] = {  1.0, 1.0, 1.0, 1.0 };
  122.  
  123.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  124.  
  125.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
  126.     glLightfv( GL_LIGHT0, GL_POSITION, light_pos );
  127.     glEnable( GL_LIGHT0 );
  128.     glEnable( GL_LIGHTING );
  129.  
  130.     glEnable( GL_DEPTH_TEST );
  131.  
  132.     quadObj = gluNewQuadric ();
  133.     gluQuadricTexture( quadObj, GL_TRUE );
  134.     gluQuadricDrawStyle( quadObj, GLU_FILL );
  135.     gluQuadricNormals( quadObj, GLU_SMOOTH );
  136. }
  137.  
  138. GLvoid 
  139. initTexture( unsigned int *image, 
  140.     GLsizei imageWidth, GLsizei imageHeight )
  141. {
  142.     GLfloat whitecol[] = {  1.0, 1.0, 1.0, 1.0 };
  143.  
  144.     /* use gluBuild2DMipmaps to create and load mipmaps (it will 
  145.      * also scale he original image to be a power of two, if 
  146.      * necessary)
  147.      *
  148.      *      gluBuild2DMipmaps( target, components, width, height,
  149.      *              format,  type, imageArray ) 
  150.      */
  151.  
  152.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  153.              GL_RGBA, GL_UNSIGNED_BYTE, image );
  154.  
  155.     /* Setting the magnification filter to nearest instead of linear 
  156.      * may run faster on some platforms, with possibly lower quality 
  157.      */
  158.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  159.  
  160.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
  161.  
  162.     /* Used when tex env mode is GL_BLEND */
  163.     glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, whitecol );
  164.  
  165.     /* enable 2D texture mapping */
  166.     glEnable( GL_TEXTURE_2D );
  167. }
  168.  
  169. GLvoid
  170. reshape( GLsizei width, GLsizei height )
  171. {
  172.     GLdouble      aspect;
  173.  
  174.     glViewport( 0, 0, width, height );
  175.  
  176.     aspect = (GLdouble) width / (GLdouble) height;
  177.  
  178.     glMatrixMode( GL_PROJECTION );
  179.     glLoadIdentity();
  180.     gluPerspective( 45.0, aspect, 1.0, 50.0 );
  181.     glMatrixMode( GL_MODELVIEW );
  182.     glLoadIdentity();
  183.     glTranslatef( 0.0, 0.0, -12.0 ); 
  184. }
  185.  
  186. GLvoid 
  187. animate( GLvoid )
  188. {
  189.     spin = fmodf( spin + 1.0, 360.0 );
  190.  
  191.     /* Tell GLUT to redraw the scene */
  192.     glutPostRedisplay();
  193. }
  194.  
  195. GLvoid
  196. visibility( int state ) 
  197. {
  198.     if (state == GLUT_VISIBLE && spinFlag) {
  199.         glutIdleFunc( animate );
  200.     } else {
  201.         glutIdleFunc( NULL );
  202.     }
  203. }
  204.  
  205. GLvoid
  206. cycleTexEnvMode( GLvoid )
  207. {
  208.     envmode = (envmode + 1) % 4;
  209.     
  210.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
  211.     printf("Texture Environment Mode is %s\n", modes[envmode].name );
  212. }
  213.  
  214. GLvoid 
  215. keyboard( GLubyte key, GLint x, GLint y )
  216. {
  217.     switch (key) {
  218.     case 'e':    /* cycle through texture environment modes */
  219.         cycleTexEnvMode();
  220.         glutPostRedisplay();
  221.         break;
  222.     case 's':    /* toggle rotation */
  223.         spinFlag = !spinFlag;
  224.         if (spinFlag)
  225.             glutIdleFunc( animate );
  226.         else
  227.             glutIdleFunc( NULL);
  228.         break;
  229.     case 't':    /* toggle rotation */
  230.         textureFlag = !textureFlag;
  231.         if (textureFlag)
  232.             glEnable( GL_TEXTURE_2D );
  233.         else
  234.             glDisable( GL_TEXTURE_2D );
  235.         glutPostRedisplay();
  236.         break;
  237.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  238.         exit(0);
  239.     }
  240. }
  241.  
  242. GLvoid
  243. drawScene(void)
  244. {
  245.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  246.  
  247.     glPushMatrix();
  248.         glRotatef( spin, 0.0, 1.0, 0.0 );
  249.         glRotatef (270.0, 1.0, 0.0, 0.0);
  250.         gluSphere(quadObj, 1.0, 16, 16);
  251.     glPopMatrix();
  252.  
  253.     glutSwapBuffers();
  254. }
  255.